import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../errors/failures.dart';

enum ViewState { initial, busy, error, success }

/// 基础控制器
abstract class BaseController extends GetxController {
  /// 加载状态
  final RxBool _isLoading = false.obs;
  
  /// 是否正在加载
  bool get isLoading => _isLoading.value;
  
  /// 显示加载中
  void showLoading() {
    _isLoading.value = true;
  }
  
  /// 隐藏加载中
  void hideLoading() {
    _isLoading.value = false;
  }
  
  /// 显示错误信息
  void showError(String message, {String? title}) {
    hideLoading();
    Get.snackbar(
      title ?? '错误',
      message,
      snackPosition: SnackPosition.BOTTOM,
      backgroundColor: Colors.redAccent,
      colorText: Colors.white,
      margin: const EdgeInsets.all(8),
      borderRadius: 10,
      duration: const Duration(seconds: 3),
    );
  }
  
  /// 显示成功信息
  void showSuccess(String message, {String? title}) {
    hideLoading();
    Get.snackbar(
      title ?? '成功',
      message,
      snackPosition: SnackPosition.BOTTOM,
      backgroundColor: Colors.green,
      colorText: Colors.white,
      margin: const EdgeInsets.all(8),
      borderRadius: 10,
      duration: const Duration(seconds: 3),
    );
  }
  
  /// 显示信息
  void showInfo(String message, {String? title}) {
    Get.snackbar(
      title ?? '提示',
      message,
      snackPosition: SnackPosition.BOTTOM,
      backgroundColor: Colors.blueAccent,
      colorText: Colors.white,
      margin: const EdgeInsets.all(8),
      borderRadius: 10,
      duration: const Duration(seconds: 3),
    );
  }

  final _state = ViewState.initial.obs;
  final _failure = Rx<Failure?>(null);

  ViewState get state => _state.value;
  Failure? get failure => _failure.value;

  void setError(Failure failure) {
    _state.value = ViewState.error;
    _failure.value = failure;
  }

  void setSuccess() {
    _state.value = ViewState.success;
  }

  void reset() {
    _state.value = ViewState.initial;
    _failure.value = null;
  }

  @override
  void onClose() {
    super.onClose();
    debugPrint('${this.runtimeType} Controller Disposed');
  }
} 